package com.ktjy.crm.controller;

import com.ktjy.crm.entity.SysUser;
import com.ktjy.crm.service.SysUserservice;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @Description
 * @Author 曹正
 * @Date 2025/1/7 13:49
 **/
@Controller
public class UserController {
    @Resource
    private SysUserservice sysUserservice;
    @RequestMapping(value = "/dologin")
    public String dologin(String usrName, String usrPassword, HttpServletRequest request){
        SysUser sysUser = sysUserservice.login(usrName, usrPassword);
        if (sysUser!=null){
            request.getSession().setAttribute("loginUser",sysUser);
            return "main";
        }else {
            request.setAttribute("message","登录失败，用户名或密码错误！");
            return "login";
        }
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");
        return "login";
    }
    @GetMapping(value = "/user/list")
    public String findUsers(String usrName, Integer roleId, Model model){
        List<SysUser> list = sysUserservice.findAllUsers();
        model.addAttribute("users",list);
        return "user/list";
    }
}
